//
//  FuncViewController.m
//  LibDemo
//
//  Created by Gikki Ares on 2020/9/5.
//

#import "MenuViewDefaultItemTestController.h"
#import <JsUtilitySdk/JsTableView.h>
#import <JsUtilitySdk/JsMenuView.h>
#import <JsUtilitySdk/JsRequestManager.h>
#import <JsUtilitySdk/JsDisplayManager.h>
#import <JsUtilitySdk/JsGeometryManager.h>
#import <JsUtilitySdk/JsRequestManager.h>
#import <JsUtilitySdk/JsColorManager.h>
#import "SlowLiveItemView.h"

@interface MenuViewDefaultItemTestController ()<
JsMenuViewDelegate
>
{
	JsMenuView * mMenuView_action;
	JsMenuView * mMenuView_display;
	UITextView * mtv;
	NSArray <SlowLiveItemVm *>* marr_display;
}

@end

#define MenuViewTagDisplay 1
#define MenuViewTagAction 2

@implementation MenuViewDefaultItemTestController

#pragma mark 01 Life Circle
- (void)viewDidLoad {
	[self setupUi];
	
	marr_display = [self generateRandomDatasource];
}

- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	NSLog(@"ViewController view frame is %@",NSStringFromCGRect(self.view.frame));
}

- (void)setupUi {
	self.view.backgroundColor = [UIColor whiteColor];
	
	{
		//显示用的menuView;
		CGRect rect = [JsGeometryManager frameHAlignLeftVAlignTopWithContainerSize:CGSizeZero size:CGSizeMake(JsGeometryManager.screenWidth, 220) offset:CGPointMake(0, 64)];
		JsMenuView * menuView = [[JsMenuView alloc]initWithFrame:rect];
		mMenuView_display = menuView;
		mMenuView_display.tag = MenuViewTagDisplay;
		menuView.backgroundColor = [UIColor lightGrayColor];
		menuView.delegate = self;
		
		[self.view addSubview:menuView];
	}
	
	{
		//操作用的menuView;
		//显示用的menuView;
		CGRect rect = [JsGeometryManager frameHAlignCenterVAlignBottomWithContainerSize:self.view.frame.size size:CGSizeMake(JsGeometryManager.screenWidth, 200)];
		mMenuView_action = [[JsMenuView alloc]initWithFrame:rect];
		mMenuView_action.tag = MenuViewTagAction;
		mMenuView_action.delegate = self;
			//TODO: 当导航栏遮挡collectionView时,点击cell会进行布局自动调整?
//		menuView.collectionView.automaticallyAdjustsScrollIndicatorInsets = YES;
//		menuView.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
			//决定self.view是否从导航栏下方开始.
			//		self.edgesForExtendedLayout = UIRectEdgeNone;
		[self.view addSubview:mMenuView_action];
		
	}
}

#pragma mark 02 Interface

#pragma mark 03 InnerFunction
- (NSMutableArray *)generateRandomDatasource {
	NSMutableArray * marr = [NSMutableArray new];
	int count = arc4random_uniform(20);
	for(int i = 0 ;i<=count;i++) {
		JsMenuItemVm * vm = [JsMenuItemVm new];
		vm.title = [NSString stringWithFormat:@"Data %d",i];
		[marr addObject:vm];
	}
	return marr;
}

# pragma mark 4 Delegate
- (NSArray<JsMenuItemVm *> *)jsMenuViewSetDatasource:(JsMenuView *)menuView {
	if(menuView.tag == MenuViewTagDisplay) {
		return marr_display;
	}
	else {
		return @[
			[[JsMenuItemVm alloc]initWithTitle:@"重新生成数据并选择第一个"],
			[[JsMenuItemVm alloc]initWithTitle:@"随机选择一个"],
			[[JsMenuItemVm alloc]initWithTitle:@"Action3"]
		];
	}
}

- (JsMenuItemStyle *)_jsMenuView:(JsMenuView *)menuView setUnselectedStyleForCellAtIndex:(int)index {
	if(menuView.tag == MenuViewTagDisplay) {
		return [JsMenuItemStyle darkModeStyle];
	}
	else {
		return nil;
	}
}

- (JsMenuItemStyle *)_jsMenuView:(JsMenuView *)menuView setSelectedStyleForCellAtIndex:(int)index {
	if(menuView.tag == MenuViewTagDisplay) {
		return [JsMenuItemStyle lightModeStyle];
	}
	else {
		return nil;
	}
}

- (JsMenuViewStyle *)jsMenuViewSetStyle:(JsMenuView *)menuView {
	if(menuView.tag == MenuViewTagDisplay) {
		JsMenuViewStyle * style = [JsMenuViewStyle defaultStyle];
		style.scrollDirection = UICollectionViewScrollDirectionVertical;
		style.initialSelectedIndex = 0;
		return style;
	}
	return nil;
}

- (void)jsMenuView:(JsMenuView *)view clickMenuAtIndex:(int)index vm:(JsMenuItemVm *)vm {
	NSLog(@"%d",index);
	if(view.tag == MenuViewTagAction) {
		switch(index) {
			case 0: {
				//重新生成数据,并选择第一个.
				marr_display = [self generateRandomDatasource];
				[mMenuView_display reload];
				[mMenuView_display selectItemAtIndex:0];
				break;
			}
			case 1: {
				//随机选择一个
				int index = arc4random_uniform((unsigned int)marr_display.count);
				NSLog(@"Select item at %d",index);
				[mMenuView_display selectItemAtIndex:index];
				break;
			}
		}
	}
	else {
		
	}
}



@end
